package com.yf.repo.modules.repo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.base.api.api.dto.BaseIdsReqDTO;
import com.yf.base.api.api.dto.PagingReqDTO;
import com.yf.repo.modules.repo.dto.request.RepoPersonReqDTO;
import com.yf.repo.modules.repo.entity.RepoPerson;
import com.yf.repo.modules.repo.mapper.RepoPersonMapper;
import com.yf.repo.modules.repo.service.RepoPersonService;
import com.yf.system.modules.user.dto.SysUserDTO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
* <p>
* 题库定员业务实现类
* </p>
*
* @author 聪明笨狗
* @since 2022-02-11 10:42
*/
@Service
public class RepoPersonServiceImpl extends ServiceImpl<RepoPersonMapper, RepoPerson> implements RepoPersonService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<String> join(BaseIdsReqDTO reqDTO) {

        // 如果已经存在了，则不重复添加
        QueryWrapper<RepoPerson> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(RepoPerson::getRepoId, reqDTO.getRefId());
        List<RepoPerson> allList = this.list(wrapper);

        // 全部ID
        List<String> allIds = new ArrayList<>();
        // 要增加的
        List<String> addIds = reqDTO.getIds();
        // 加入数据为空
        if(CollectionUtils.isEmpty(addIds)){
            addIds = new ArrayList<>();
        }

        if(!CollectionUtils.isEmpty(allList)){
            for (RepoPerson item: allList){
                // 现有的全部列表
                allIds.add(item.getUserId());
                // 移除已有的
                addIds.remove(item.getUserId());
            }
        }

        if(CollectionUtils.isEmpty(addIds)){
            return allIds;
        }

        List<RepoPerson> list = new ArrayList<>();
        for(String id: addIds){
            RepoPerson person = new RepoPerson();
            person.setRepoId(reqDTO.getRefId());
            person.setUserId(id);
            list.add(person);

            // 增加范湖数据
            allIds.add(id);
        }

        this.saveBatch(list);

        return allIds;
    }

    @Override
    public void remove(BaseIdsReqDTO reqDTO) {

        // 移除数据
        QueryWrapper<RepoPerson> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(RepoPerson::getUserId, reqDTO.getIds())
                .eq(RepoPerson::getRepoId, reqDTO.getRefId());
        this.remove(wrapper);
    }

    @Override
    public IPage<SysUserDTO> paging(PagingReqDTO<RepoPersonReqDTO> reqDTO) {
        return baseMapper.paging(reqDTO.toPage(), reqDTO.getParams());
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void modifyAll(String tempId, String prodId) {

        // 无需操作
        if(!tempId.startsWith("temp_")){
            return;
        }

        // 修改引用关系
        QueryWrapper<RepoPerson> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(RepoPerson::getRepoId, tempId);

        RepoPerson person = new RepoPerson();
        person.setRepoId(prodId);

        this.update(person, wrapper);
    }

}
